package cn.zwxky.android.staff.Approve;

import android.content.Context;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.OnClick;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.config.Config;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.utils.SN;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.zwxky.android.staff.Login.ForgetBean;
import cn.zwxky.android.staff.R;
import cn.zwxky.android.staff.Utils.ListViewForScrollView;
import cn.zwxky.android.staff.Utils.LittleUtils;
import cn.zwxky.android.staff.Utils.NoDoubleClickUtils;
import cn.zwxky.android.staff.Utils.URLString;

/**
 * 项目审批讨论页面
 * Created by Administrator on 2017/3/31 0031.
 */
@ContentView(R.layout.aty_task_details_chat)
public class TaskDetailsChatActivity extends LsfbActivity {


    /**
     * 头像
     */
    @ViewInject(R.id.aty_img_head)
    private SimpleDraweeView img_head;

    /**
     * 名字
     */
    @ViewInject(R.id.aty_text_name)
    private TextView text_name;

    /**
     * 电话
     */
    @ViewInject(R.id.aty_text_phone)
    private TextView text_phone;

    /**
     * 公司
     */
    @ViewInject(R.id.aty_text_gongsi)
    private TextView text_gongsi;

    /**
     * 项目名称
     */
    @ViewInject(R.id.aty_text_title)
    private TextView text_title;

    /**
     * 备注
     */
    @ViewInject(R.id.aty_text_beizhu)
    private TextView text_beizhu;

    /**
     * 项目类型
     */
    @ViewInject(R.id.aty_text_type)
    private TextView text_type;

    /**
     * 输入框
     */
    @ViewInject(R.id.aty_chat_et_import)
    private EditText et_import;

//    /**
//     * 点击刷新布局控件
//     */
//    @ViewInject(R.id.swiplayout)
//    private SwipeRefreshLayout swipeRefreshLayout;
//
//    /**
//     * list列表
//     */
//    @ViewInject(R.id.dish_list)
//    private RecyclerView dish_list;
//
//    /**
//     * RecyclerView的布局管理器
//     */
//    private RecyclerView.LayoutManager mlayoutManager;

    @ViewInject(R.id.dish_list)
    private ListViewForScrollView dish_list;

    private List<TaskDetailsChatBean.ListBean> mList;

    private TaskDetailsChatAdapter adapter;


    private String id;

    private Handler handler = new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            switch (msg.what) {
                case 1000:
                    et_import.setText("");
                    break;
            }
        }
    };

    @Override
    public void initView() {
        getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        LittleUtils.setsimpletitlebar(this, "讨论");
        id = getIntent().getStringExtra("id");

        mList = new ArrayList<>();
        adapter = new TaskDetailsChatAdapter(this, R.layout.item_follow, mList);
//        mlayoutManager = new LinearLayoutManager(this);//布局模式为listview方式
//        dish_list.setLayoutManager(mlayoutManager);
        dish_list.setAdapter(adapter);

//        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//            @Override
//            public void onRefresh() {
//                getData();
//            }
//        });
    }

    @Override
    public void initEvent() {

    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.aty_chat_btn_post})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.aty_chat_btn_post:
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(et_import.getWindowToken(), 0);
                if (!NoDoubleClickUtils.isDoubleClick()) {
                    post();
                }
                break;
        }
    }

    @Override
    public void initData() {
        getData();
    }

    public void getData() {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("uid", Config.getUserId());
        hashMap.put("id", id);
        new BaseInternet().getData_Get(URLString.MYEAATALK, hashMap, TaskDetailsChatBean.class, new TaskDetailsChatEvent<TaskDetailsChatBean>(), true, true);
    }

    @EventAnnotation
    public void TaskDetailsChatEvent(TaskDetailsChatEvent<TaskDetailsChatBean> event) {
//        swipeRefreshLayout.setRefreshing(false);
        switch (event.getCode()) {
            case 200:
                img_head.setImageURI(Uri.parse(URLString.IMG_URL + event.getData().getImg()));
                text_name.setText(event.getData().getName());
                text_phone.setText(event.getData().getTel());
                text_gongsi.setText(event.getData().getCompany());
                text_title.setText(event.getData().getProname());
                text_beizhu.setText(event.getData().getContent());
                text_type.setText(event.getData().getCla_name());

                mList.clear();
                mList.addAll(event.getData().getList());
                adapter.notifyDataSetChanged();
                break;
            default:
                SN.SnackShow(dish_list, event.getMsg());
                break;
        }
    }

    public void post() {
        if (et_import.getText().toString().isEmpty()) {
            SN.SnackShow(dish_list, "请输入内容");
            return;
        }
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("uid", Config.getUserId());
        hashMap.put("id", id);
        hashMap.put("content", et_import.getText().toString());
        new BaseInternet().getData(URLString.MANAGE_EAATALK, hashMap, ForgetBean.class, new TaskDetailsChatPostEvent<ForgetBean>(), false);
    }

    @EventAnnotation
    public void TaskDetailsChatPostEvent(TaskDetailsChatPostEvent<ForgetBean> event) {
        switch (event.getCode()) {
            case 200:

                et_import.setText("");
                SN.SnackShow(dish_list, event.getMsg());
                handler.sendEmptyMessage(1000);
                getData();
                break;
            default:
                SN.SnackShow(dish_list, event.getMsg());
                break;
        }
    }


}
